
package ch.hearc.coursJava.moo.chrono;

public class Chrono
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/

	public Chrono()
		{
		this.isRunning = false;
		this.totalMillis = 0;
		this.lastStart = 0;
		start();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	public void start()
		{
		if (!isRunning)
			{
			isRunning = true;
			lastStart = System.currentTimeMillis();
			}
		}

	public void pause()
		{
		if (isRunning)
			{
			isRunning = false;
			totalMillis = totalMillis + System.currentTimeMillis() - lastStart;
			}
		}

	public void stop()
		{
		isRunning = false;
		totalMillis = 0;
		lastStart = 0;
		}

	@Override
	public String toString()
		{
		StringBuilder builder = new StringBuilder();
		builder.append("Chrono time: ");
		builder.append(getTime());
		return builder.toString();
		}

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	public long getTime()
		{
		if (!isRunning)
			{
			return totalMillis;
			}
		else
			{
			return totalMillis + System.currentTimeMillis() - lastStart;
			}
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	// tools
	private boolean isRunning;
	private long totalMillis;
	private long lastStart;
	}
